﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BUS;
namespace AirPlane.UserConTrol
{
    public partial class Wuc_TiGia : System.Web.UI.UserControl
    {
       string Url = "http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx";
       protected TienTeBUS bus = new TienTeBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["update"] = "1";
            load();
           
        }
        public void load()
        {
            string s1 = "";
         
            s1 = "<table id ='table-b-ti-gia'>";
            s1 += "<tr >";
            s1 += "<th>Ngoại Tệ </th>";
            s1 += "<th>Tỉ Giá</th>";
            s1 += "</tr>";
            DataSet ds = new DataSet();


            ds.ReadXml(Url);

            if (ds != null && ds.Tables.Count > 0)
            {

                DataTable dt = new DataTable();
                bool flag = true;
                dt = ds.Tables["Exrate"];
                string sg = "";
                float tigia = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    string tenNgoaiTe = dr["CurrencyCode"].ToString();
                    string giaBan = dr["Sell"].ToString();
                    if (tenNgoaiTe == "SGD")
                    {
                        sg = tenNgoaiTe;
                        tigia = float.Parse(giaBan);
                    }

                }
                foreach (DataRow dr in dt.Rows)
                {
                    string tenNgoaiTe = dr["CurrencyCode"].ToString();
                    string giaBan = dr["Sell"].ToString();
                   
                        if (tenNgoaiTe != "SGD")
                        {
                            s1 += "<tr >";
                            s1 += "<td>" + tenNgoaiTe + " </td>";
                            s1 += "<td>" + Math.Round(float.Parse(giaBan) / tigia, 5) + "</td>";
                            s1 += "</tr>";
                            if (DateTime.Now.Hour == 8 || DateTime.Now.Hour == 10 || DateTime.Now.Hour == 12 || DateTime.Now.Hour == 14 ||
                                DateTime.Now.Hour == 17 || DateTime.Now.Hour == 18 || DateTime.Now.Hour == 20 || DateTime.Now.Hour == 23)
                            {
                                if (Session["update"].ToString() == "1")
                                {
                                    bus.update(tenNgoaiTe, (float)Math.Round(float.Parse(giaBan) / tigia, 5));
                                    if (flag)
                                    {
                                        bus.update("VND", (float)Math.Round(1 / tigia, 5));
                                        flag = false;

                                    }
                                    Session["update"] = "2";
                                }

                            }
                            else
                            {
                                Session["update"] = "1";
                            }
                        }
                    

                }
                double vnd = Math.Round(1 / tigia, 5);
                s1 += "<tr >";
                s1 += "<td>VND</td>";
                s1 += "<td>1/" + tigia + "</td>";
                s1 += "</tr>";
                s1 += "<tr>";
                s1 +="  <td colspan='2' >";
                s1 += "<span style = 'font-size:12px;font-style:italic;font-weight:bold'> Nguồn VietComBank </span>";
                s1 += "</td> </tr>";
                s1 += "</table>";
               
                lbTiGia.Text = s1;
            }
        }
    }
}